/**
 * Ingenius-Group S.A.S
 *
 * Project: SAU
 * File: ListQuestionsView.java
 * Date: 04/02/2011
 */

package com.ingenius.sau.client.view;

import java.util.ArrayList;
import java.util.List;

import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.widget.Html;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.ui.DisclosurePanel;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HasAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.ingenius.sau.client.Constants;
import com.ingenius.sau.client.presenter.ListQuestionsPresenter.ListQuestionsDisplay;
import com.ingenius.sau.client.view.widgets.LinkImage;
import com.ingenius.sau.client.view.widgets.LinkImage.LinkImageStatus;
import com.ingenius.sau.client.view.widgets.ListBox;
import com.ingenius.sau.shared.model.instruments.Question;
import com.ingenius.sau.shared.util.filter.Comparison;
import com.ingenius.sau.shared.util.filter.Filter;
import com.ingenius.sau.shared.util.filter.StringFilter;

/**
 * Class <strong>ListQuestionsView</strong>
 * 
 * @author Jesus David
 * @version 0.1
 */
public class ListQuestionsView extends AbstractListView<Question> implements
		ListQuestionsDisplay {

	// Singleton
	private static ListQuestionsView instance;
	// UI Widgets
	private TextField<String> filterTitle = new TextField<String>();
	private TextField<String> filterContent = new TextField<String>();
	private ListBox filterType = new ListBox();

	private LinkImage link1;
	private LinkImage link2;

	private ListQuestionsView() {
		super();
		link1 = new LinkImage("Documents", Constants.ICON_DOCUMENTS);
		link2 = new LinkImage("Questions", Constants.ICON_QUESTIONS);
	}

	public static ListQuestionsView getInstance() {
		if (instance == null) {
			instance = new ListQuestionsView();
		}
		return instance;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.ingenius.sau.client.view.AbstractListView#onRender(com.google.gwt
	 * .user.client.Element, int)
	 */
	@Override
	protected void onRender(Element parent, int index) {
		super.onRender(parent, index);
		// TODO i18n
		gridContainer.setHeading("Questions");
		configureHeader();
		configureSubmenu();

		filterType.setEditable(false);
		filterType.add("---");
		filterType.add(Question.TYPE_MULTIPLE_RESPONSE);
		filterType.add(Question.TYPE_ONLY_RESPONSE);
		filterType.add(Question.TYPE_OPEN_RESPONSE);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.ingenius.sau.client.mvp.views.AbstractListView#createColumnModel()
	 */
	@Override
	protected ColumnModel createColumnModel() {
		List<ColumnConfig> columns = new ArrayList<ColumnConfig>();
		// TODO i18n 2º columna
		ColumnConfig column = new ColumnConfig("title", "Title", 100);
		columns.add(column);

		column = new ColumnConfig("content", "Content", 200);
		columns.add(column);

		column = new ColumnConfig("type", "Type", 100);
		columns.add(column);

		column = new ColumnConfig("options", "Options", 250);
		column.setSortable(false);
		columns.add(column);

		return new ColumnModel(columns);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.ingenius.sau.client.mvp.views.AbstractListView#createAdvancedSearch()
	 */
	@Override
	protected Widget createAdvancedSearch() {
		// TODO i18n
		final DisclosurePanel panel = new DisclosurePanel("Advanced search");
		panel.setStyleName("disclosure-panel");
		panel.setAnimationEnabled(false);

		FlexTable table = new FlexTable();
		table.setStyleName("advanced-search");
		table.setCellSpacing(11);
		table.setCellPadding(11);

		// TODO i18n
		table.setHTML(0, 0, "Title");
		table.setWidget(0, 1, filterTitle);

		table.setHTML(0, 2, "Content");
		table.setWidget(0, 3, filterContent);

		table.setHTML(0, 4, "Type");
		table.setWidget(0, 5, filterType);

		HorizontalPanel buttons = new HorizontalPanel();
		buttons.setSpacing(10);
		buttons.setHorizontalAlignment(HasAlignment.ALIGN_CENTER);

		// TODO i18n
		Button searchButton = new Button("Search");
		searchButton.addListener(Events.OnClick, new Listener<BaseEvent>() {
			@Override
			public void handleEvent(BaseEvent be) {
				doSearch();
			}
		});
		// TODO i18n
		Button cancelButton = new Button("Cancel");
		cancelButton.addListener(Events.OnClick, new Listener<BaseEvent>() {
			@Override
			public void handleEvent(BaseEvent be) {
				doCancel();
				panel.setOpen(false);
			}
		});

		buttons.add(searchButton);
		buttons.add(cancelButton);

		VerticalPanel wrap = new VerticalPanel();
		wrap.add(table);
		wrap.add(buttons);
		wrap.setCellVerticalAlignment(buttons, HasAlignment.ALIGN_BOTTOM);
		wrap.setCellHorizontalAlignment(buttons, HasAlignment.ALIGN_CENTER);
		panel.add(wrap);

		return panel;
	}

	private void doSearch() {
		if (filters == null) {
			filters = new ArrayList<Filter>();
		}
		filters.clear();

		if (filterTitle.getValue() != null) {
			filters.add(new StringFilter(Comparison.EQUALS, "title",
					filterTitle.getValue()));
		}
		if (filterContent.getValue() != null) {
			filters.add(new StringFilter(Comparison.EQUALS, "content",
					filterContent.getValue()));
		}
		if (filterType.getSimpleValue() != null
				&& !filterType.getSimpleValue().equals("---")) {

			filters.add(new StringFilter(Comparison.EQUALS, "type", filterType
					.getSimpleValue()));
		}
		super.reload();
	}

	private void doCancel() {
		filterTitle.setValue("");
		filterContent.setValue("");
		filterType.setSimpleValue("---");

		if (filters != null) {
			filters.clear();
			super.reload();
		}
	}

	protected void configureHeader() {
		// TDOD i18n
		String moduleName = "Questions";
		String html = "<span class='module-header'>" + moduleName + "</span>";
		headerContainer.add(new Html(html));
	}

	protected void configureSubmenu() {
		link1.addListener(Events.OnClick, new Listener<BaseEvent>() {
			@Override
			public void handleEvent(BaseEvent be) {
				if (link1.getStatus().equals(LinkImageStatus.enable)) {
					History.newItem(Constants.TOKEN_DOCUMENTS);
				}
			}
		});

		link1.setStatus(LinkImageStatus.enable);
		link2.setStatus(LinkImageStatus.active);

		submenuContainer.add(link1);
		submenuContainer.add(link2);
	}

}
